package com.tiger.project.service.impl;

import java.util.Arrays;
import java.util.List;

import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.*;
import com.tiger.project.mapper.*;
import com.tiger.project.service.IContractBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【合同账单】Service业务层处理
 *
 * @author standard
 * @date 2022-09-26
 */
@Service
public class ContractBillServiceImpl implements IContractBillService {
    @Autowired
    private ContractBillMapper contractBillMapper;
    @Autowired
    private CommonDecisionMapper decisionMapper;
    @Autowired
    private ContractSponsorCreditMapper creditMapper;
    @Autowired
    private PayInfoMapper payInfoMapper;
    @Autowired
    private PayInfoEnclosureMapper payInfoEnclosureMapper;

    /**
     * 查询【合同账单】
     *
     * @param id 【合同账单】主键
     * @return 【合同账单】
     */
    @Override
    public ContractBill selectContractBillById(Long id) {
        ContractBill contractBill = contractBillMapper.selectContractBillById(id);
        info(contractBill);
        return contractBill;
    }

    /**
     * 删除开票信息，把该条开票信息置空
     * @param id
     * @return
     */
    @Override
    public int deletePayee(Long id) {
        return contractBillMapper.deleteInvoice(id);
    }


    /**
     * 修改【合同账单】
     *
     * @param contractBill 【合同账单】
     * @return 结果
     */
    @Override
    public int updateContractBill(ContractBill contractBill) {
        return contractBillMapper.updateContractBill(contractBill);
    }



    /**
     * 收款预警列表/开票申请
     * @param contractBill
     * @return
     */
    @Override
    public List<ContractBill> payeeEarlyList(ContractBill contractBill) {
        return contractBillMapper.payeeEarlyList(contractBill);
    }

    /**
     *
     * @param id 账期id
     * @param status 审核状态
     * @param oaFile oa文件链接
     * @param isMakeInvoice isMakeInvoice
     * @param isGetInvoice isGetInvoice
     * @return
     */
    @Override
    public int callBack(Long id, Long status, String oaFile, String isMakeInvoice, String isGetInvoice) {
        ContractBill bill = new ContractBill();
        bill.setId(id);
        bill.setStatus(status);
        bill.setOaFile(oaFile);
        bill.setIsMakeInvoice(isMakeInvoice);
        bill.setIsGetInvoice(isGetInvoice);
        bill.setRecordStatus(0L);
        bill.setArrivalStatus(0L);
        return contractBillMapper.updateContractBill(bill);
    }


    /**
     * 租金进账  添加缴费
     * @param contractBill
     * @return
     */
    @Override
    public int editRecord(ContractBill contractBill) {
        payInfoMapper.deletePayInfo(contractBill.getId(),contractBill.getPayType());
        payInfoEnclosureMapper.deletePayInfo(contractBill.getId());
        if (StringUtils.isNotEmpty(contractBill.getPayInfoList())){
            contractBill.getPayInfoList().forEach(info ->{
                info.setBillId(contractBill.getId());
                payInfoMapper.insertPayInfo(info);
                //缴费/到账附件
                if (StringUtils.isNotEmpty(info.getPayInfoEnclosureList())){
                    info.getPayInfoEnclosureList().forEach(item ->{
                        item.setBillId(contractBill.getId());
                        item.setPayInfoId(info.getId());
                        item.setCreatedAt(DateUtils.getNowDate());
                        payInfoEnclosureMapper.insertPayInfoEnclosure(item);
                    });
                }
            });
        }
        return contractBillMapper.updateContractBill(contractBill);
    }

    /**
     * 查看 租金进账和租金到账详情
     * @param id 账期id
     * @return
     */
    @Override
    public ContractBill getInfo(Long id,Long type) {
        ContractBill contractBill = contractBillMapper.selectContractBillById(id);
        info(contractBill);
        PayInfo payInfo = new PayInfo();
        payInfo.setBillId(id);
        payInfo.setPayType(type);
        List<PayInfo> infoList = payInfoMapper.selectPayInfoList(payInfo);
        if (StringUtils.isNotEmpty(infoList)){
            infoList.forEach(info->{
                PayInfoEnclosure enclosure = new PayInfoEnclosure();
                enclosure.setBillId(id);
                enclosure.setPayInfoId(info.getId());
                info.getPayInfoEnclosureList().addAll(payInfoEnclosureMapper.selectPayInfoEnclosureList(enclosure));
            });
        }
        contractBill.getPayInfoList().addAll(infoList);
        return contractBill;
    }

    /**
     * 删除租金到账
     * @param id 账期id
     * @return
     */
    @Override
    public int deleteRecord(Long id) {
        return contractBillMapper.deleteRecord(id);
    }

    /**
     * 删除租金进账
     * @param id
     * @return
     */
    @Override
    public int deleteArrival(Long id) {
        return contractBillMapper.deleteArrival(id);
    }

    /**
     * 租金到账审批
     * @param id
     * @param recordStatus 审批状态
     * @param oaFile 审批文件
     * @return
     */
    @Override
    public int callBackRecord(Long id, Long recordStatus, String  oaFile) {
        ContractBill bill = new ContractBill();
        bill.setId(id);
        bill.setRecordStatus(recordStatus);
        bill.setOaFileRecord(oaFile);
        return contractBillMapper.updateContractBill(bill);
    }


    /**
     * 租金进账审批
     * @param id
     * @param arrivalStatus 审批状态
     * @param oaFile 审批处文件
     * @return
     */
    @Override
    public int callBackArrival(Long id,  Long arrivalStatus, String oaFile ) {
        ContractBill bill = new ContractBill();
        bill.setId(id);
        bill.setArrivalStatus(arrivalStatus);
        bill.setOaFileArrival(oaFile);
        return contractBillMapper.updateContractBill(bill);
    }


    private void info (ContractBill contractBill){
        //开票信息决策依据
        if (StringUtils.isNotEmpty(contractBill.getDecisionId())){
            for (String s : Arrays.asList(contractBill.getDecisionId().split(","))) {
                CommonDecision commonDecision = decisionMapper.selectCommonDecisionById(Long.valueOf(s));
                contractBill.getDecisionList().add(commonDecision);
            }
        }
        //减免信息
        ContractSponsorCredit credit = new ContractSponsorCredit();
        credit.setContractSponsorId(contractBill.getContractSponsorId());
        credit.setRentPeriods(contractBill.getRentPeriods());
        credit.setStatus(3L);
        List<ContractSponsorCredit> credits = creditMapper.selectContractSponsorCreditList(credit);
        if (StringUtils.isNotEmpty(credits)){
            contractBill.getCreditList().addAll(credits);
        }
    }


}
